package cn.ljn.system.controller;


import cn.ljn.basic.util.AjaxResult;
import cn.ljn.basic.util.PageList;
import cn.ljn.system.domain.SystemDetail;
import cn.ljn.system.domain.SystemType;
import cn.ljn.system.query.SystemTypeQuery;
import cn.ljn.system.service.ISystemDetailService;
import cn.ljn.system.service.ISystemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system")
public class SystemTypeController {
    @Autowired
    private ISystemDetailService iSystemDetailService;
    @Autowired
    private ISystemTypeService iSystemTypeService;
    // 查询全部
    @GetMapping
    public List<SystemType> findAll(){
        return iSystemTypeService.findAll();
    }

    // 根据id查询
    @GetMapping("/{id}")
    public SystemType findById(@PathVariable("id") Long id){
        return iSystemTypeService.findById(id);
    }
    // 高级查询
    @PostMapping
    public PageList<SystemType> queryByPage(@RequestBody SystemTypeQuery systemTypeQuery){
            return iSystemTypeService.queryByPage(systemTypeQuery);
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        List<SystemDetail> byId = iSystemDetailService.getDetail(id);
        //联级删除
        List<Long> detailIds = byId.stream().map(SystemDetail::getId).collect(Collectors.toList());
        iSystemDetailService.batchDelete(detailIds);
        iSystemTypeService.delete(id);
        return AjaxResult.me().success();
    }
    // 批量删除
    @PatchMapping("/{ids}")
    public AjaxResult batchDelete(@PathVariable("ids") List<Long> ids){
            iSystemTypeService.batchDelete(ids);
            return AjaxResult.me().success();
        }
    // 添加修改
    @PutMapping
    public AjaxResult save(@RequestBody SystemType systemType){
            if(systemType.getId() == null){
                iSystemTypeService.add(systemType);
        }else{
                iSystemTypeService.update(systemType);
            }
            return AjaxResult.me().success();
    }

}